//
//  SINAStuActivityAddItemViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2018/1/18.
//  Copyright © 2018年  androidlongs. All rights reserved.
//  添加活动页面
//

#import "SINAStuActivityAddItemViewController.h"
#import "SINAUiviewClickLiserner.h"
#import "SINACircularBeadUtils.h"
#import "SINAContentScrollView.h"

@interface SINAStuActivityAddItemViewController ()<UIScrollViewDelegate>

//添加规划
@property (weak, nonatomic) IBOutlet UILabel *addPlanLabel;
@property (weak, nonatomic) IBOutlet UIView *addPlanView;

//添加自定义
@property (weak, nonatomic) IBOutlet UILabel *addCustomLabel;
@property (weak, nonatomic) IBOutlet UIView *addCustomView;
//头部指示线
@property (weak, nonatomic) IBOutlet UIView *addItemFlagView;
//指示线的宽度
@property(assign,nonatomic) CGFloat divix;
@property (weak, nonatomic) IBOutlet UIImageView *titleFlagImageView;


//内容页面
@property (weak, nonatomic) IBOutlet SINAContentScrollView *contentScrollView;

@property(strong,nonatomic) NSArray *topDataList;

@end

@implementation SINAStuActivityAddItemViewController
-(NSArray *)topDataList{
    if (!_topDataList) {
        _topDataList= @[@"我的委托",@"我的兼职"];
    }
    return _topDataList;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"添加活动";
    self.navigationController.navigationBarHidden = NO;
    if (UI_IS_IPHONE6PLUS) {
        self.titleFlagImageView.image = [UIImage imageNamed:@"nav_bar_plus_bg"];
    } else {
        self.titleFlagImageView.image = [UIImage imageNamed:@"nav_bar_bg"];
    }
    [super initBackItem];
    [self initViewFunction];
    [self initHeaderFunction];
    [self initOtherController];
}
-(void) initViewFunction{
    self.contentScrollView.showsVerticalScrollIndicator=NO;
    self.contentScrollView.scrollEnabled=NO;
    self.contentScrollView.showsHorizontalScrollIndicator=NO;
    //[SINACircularBeadUtils makeCircularBead:2 andView:self.commitView andIsSetBackground:NO];
    self.addItemFlagView.backgroundColor=baseSelectColor;
}
-(void) initHeaderFunction{
    __weak typeof(self) weakSelf = self;
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.addPlanView andWithBlick:^(UIView *view) {
        DLog(@"添加规划");
        [self.view endEditing:YES];
        CGPoint point = CGPointMake(0, self.contentScrollView.contentOffset.y);
        [self.contentScrollView setContentOffset:point animated:YES];
        [UIView animateWithDuration: 0.5 animations: ^{
            weakSelf.addItemFlagView.centerX = weakSelf.addPlanLabel.centerX;
            weakSelf.addPlanLabel.textColor = baseSelectColor;
            weakSelf.addCustomLabel.textColor=[UIColor colorWithHexString:@"#565656"];
        } completion: nil];
    }];
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.addCustomView andWithBlick:^(UIView *view) {
        DLog(@"添加自定义");
         [self.view endEditing:YES];
        CGPoint point = CGPointMake(mainW, self.contentScrollView.contentOffset.y);
        [self.contentScrollView setContentOffset:point animated:YES];
        [UIView animateWithDuration: 0.5 animations: ^{
            weakSelf.addItemFlagView.centerX = weakSelf.addCustomLabel.centerX;
            weakSelf.addCustomLabel.textColor = baseSelectColor;
            weakSelf.addPlanLabel.textColor=[UIColor colorWithHexString:@"#565656"];
        } completion: nil];
    }];
}

-(void)initOtherController{
    NSArray *array = @[@"SINAStuAcitivtyAddPlanViewController",@"SINAStuAcitivtyAddCustomViewController"];
    for (NSInteger i=0; i<array.count; i++) {
        //取出数据
        NSString *item = array[i];
        //创建
        UIViewController *controller = [[NSClassFromString(item) alloc] init];
        
        [self addChildViewController:controller];
    }
    //将子控制器的View添加到MainView的scrollView中
    //4 设置scrollview的contentsize
    self.contentScrollView.contentSize=CGSizeMake([UIScreen mainScreen].bounds.size.width*self.topDataList.count, 0);
    //5 设置uiscrollView的代理
    self.contentScrollView.delegate=self;
    self.contentScrollView.pagingEnabled=YES;
    //进入主控制器的第一个页面
    [self scrollViewDidEndDecelerating:self.contentScrollView];
    self.divix=self.addItemFlagView.centerX;
}
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    //每一个子控制器的宽度
    CGFloat width =scrollView.frame.size.width;
    //每一个子控制器的高度
    CGFloat height =CGRectGetHeight(self.contentScrollView.frame);
    //获取当前索引值
    NSInteger idx = scrollView.contentOffset.x/width;
    
    //设置联动
    [self selectPage:idx];
    //[self.topBarView scrollChangTopBar:idx];
    //根据索引值返回引用
    UIViewController *vc=self.childViewControllers[idx];
    
    //判断当前的vc是否执行过viewLoaded
    if([vc isViewLoaded]){
        return;
    }else{
        //设置子控制器的视图大小
        vc.view.frame=CGRectMake(scrollView.contentOffset.x, 0, scrollView.frame.size.width, height);
        //将子控制器的view加入到scrollview中
        [scrollView addSubview:vc.view];
    }
}
//滑动减速结束
//调用加载子控制器的view的方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    //DLog(@"滑动结束 ...");
    [self scrollViewDidEndScrollingAnimation:scrollView];
    [self.view endEditing:YES];
}
//scrollView滚动时，就调用该方法。任何offset值改变都调用该方法。即滚动过程中，调用多次

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //DLog(@"scrollViewDidScroll");
    
    CGPoint point=scrollView.contentOffset;
    
    //DLog(@"%f,%f",point.x,point.y);
    
    
    CGFloat leftX = self.addPlanView.centerX;
    CGFloat rightX = self.addCustomView.centerX;
    
    CGFloat offsetX=rightX-leftX;
    
    self.addItemFlagView.centerX= self.divix+point.x/mainW*offsetX;
    
}

-(void)selectPage:(NSInteger) flag{
    __weak typeof(self) weakSelf = self;
    if (flag==0) {
        [UIView animateWithDuration: 0.5 animations: ^{
            weakSelf.addItemFlagView.centerX = weakSelf.addPlanLabel.centerX;
            weakSelf.addPlanLabel.textColor = baseSelectColor;
            weakSelf.addCustomLabel.textColor=[UIColor colorWithHexString:@"#565656"];
        } completion: nil];
    } else {
        [UIView animateWithDuration: 0.5 animations: ^{
            weakSelf.addItemFlagView.centerX = weakSelf.addCustomLabel.centerX;
            weakSelf.addCustomLabel.textColor =baseSelectColor;
            weakSelf.addPlanLabel.textColor=[UIColor colorWithHexString:@"#565656"];
        } completion: nil];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
